<template>
  <el-dialog
    :model-value="dialogVisible"
    :title="dialogTitle"
    width="40%"
    @close="handleClose"
  >
    <el-form :model="form" label-width="100px" :rules="rules" ref="formRef">
      <!--      <el-form-item label="这是收款单" prop="type" :formatter="formatType" >-->
      <!--        <el-input v-model="form.type"/>-->
      <!--      </el-form-item>-->

      <!--      <el-form-item label="这是收款单" prop="type" >-->
      <!--        <el-radio v-model="form.type" label="1" type="hidden"></el-radio>-->
      <!--        <el-radio v-model="form.type" label="2" type="display:none"></el-radio>-->
      <!--      </el-form-item>-->

      <el-form-item label="收支金额" prop="inOutAmount">
        <el-input v-model="form.inOutAmount" />
      </el-form-item>
      <el-form-item label="收支描述" prop="dec">
        <el-input
          v-model="form.dec"
          :rows="2"
          type="textarea"
          placeholder="输入明细描述"
        />
      </el-form-item>
      <el-form-item label="所属门店" prop="storeId">
        <el-select v-model="form.storeId" clearable placeholder="请选择">
          <el-option
            v-for="item in options"
            :key="item.id"
            :label="item.companyName"
            :value="item.id"
            :disabled="item.disabled">
          </el-option>
        </el-select>
      </el-form-item>
    </el-form>
    <template #footer>
        <span class="dialog-footer">
          <el-button type="primary" @click="handleConfirm">添加</el-button>
          <el-button @click="handleClose">取消</el-button>
        </span>
    </template>
  </el-dialog>
</template>

<script setup>
import { defineEmits, ref, defineProps, watch, onMounted } from 'vue'
import { saveList, modify } from '../../../api/expenditure'
import { listAll } from '../../../api/company'
import { ElMessage } from 'element-plus'

const form = ref({
  id: '',
  type: '',
  inOutAmount: '',
  dec: '',
  storeId: ''
})
// 所属门店
const options = ref([])
async function query3() {
  const row = await listAll()
  options.value = row.data.data
}

// vue组件加载完成执行
onMounted(() => {
  query3()
})
const props = defineProps({
  // 标题
  dialogTitle: {
    type: String,
    default: '',
    required: true
  },
  // 编辑行数据
  dialogRowValue: {
    type: Object,
    default: () => {}
  }
})
// 监听props.dialogRowValue值改变
watch(
  () => props.dialogRowValue,
  () => {
    // console.log(props.dialogRowValue)
    form.value = props.dialogRowValue
  },
  { deep: true, immediate: true }
)

const isDisabled = ref(true)
onMounted(() => {
  if (props.dialogTitle === '编辑') {
    isDisabled.value = true
  }
})
// function formatType(row, column) {
//   const status = row[column.property]
//   if (status === '1') {
//     return '收入'
//   } else if (status === '2') {
//     return '支出'
//   }
// }
// 表单校验
const rules = ref({
  s: [
    { required: true, message: '收支类型不能为空' }
  ],
  inOutAmount: [
    { required: true, message: '收支金额不能为空' }
  ],
  dec: [
    { required: true, message: '收支描述不能为空' }
  ],
  storeId: [
    { required: true, message: '选择所属门店不能为空' }
  ]
})

const emits = defineEmits(['update:modelValue', 'query'])

// 关闭
function handleClose() {
  emits('update:modelValue', false)
}
// 确认
const formRef = ref(null)
function handleConfirm() {
  formRef.value.validate(async (valid) => {
    if (valid) {
      props.dialogTitle === '添加' ? await saveList(form.value) : await modify(form.value)
      ElMessage({
        message: '更新成功',
        type: 'success'
      })
      // 上层组件重新获取数据
      emits('query')
      handleClose()
    } else {
      return false
    }
  })
}
</script>

<style lang="scss" scoped>
::v-deep .el-input__prefix-inner {
  align-items: center;
}
</style>
